package com.xomily.devops.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xomily.common.utils.sign.Md5Utils;
import com.xomily.devops.domain.DevAlarmLog;
import com.xomily.devops.domain.DevHost;
import com.xomily.devops.mapper.DevAlarmLogMapper;
import com.xomily.devops.mapper.DevCpuLogMapper;
import com.xomily.devops.mapper.DevDiscLogMapper;
import com.xomily.devops.mapper.DevHostMapper;
import com.xomily.devops.mapper.DevMemLogMapper;
import com.xomily.devops.mapper.DevNetLogMapper;
import com.xomily.devops.service.IDevAgentService;
import com.xomily.devops.service.IDevAlarmLogService;
import com.xomily.system.service.ISysConfigService;

/**
 * 信息收集Service业务层处理
 * 
 * @author xomily
 * @date 2021-11-29
 */
@Service
public class DevAgentServiceImpl implements IDevAgentService {
	@Autowired
	private DevHostMapper devHostMapper;
	@Autowired
	private ISysConfigService configService;
	@Autowired
	private IDevAlarmLogService alarmLogService;
    @Autowired
    private DevDiscLogMapper discLogMapper;
    @Autowired
    private DevCpuLogMapper cpuLogMapper;
    @Autowired
    private DevMemLogMapper memLogMapper;
    @Autowired
    private DevNetLogMapper netLogMapper;
    @Autowired
    private DevAlarmLogMapper alarmLogMapper;
    
	/**
	 * 更新主机状态
	 * 
	 * @param 无
	 * @return 状态码
	 */
	@Override
	public int updateHostByTask() {
		int offlineTimes =  Integer.valueOf(configService.selectConfigByKey("dev.host.offlineTimes"));
		if (offlineTimes == 0)
			return 0;
		int n = (-1) * offlineTimes;
		Date now = com.xomily.common.utils.DateUtils.getNowDate();
		Date t = DateUtils.addMinutes(now, n);
		List<DevHost> hostList = devHostMapper.selectDevHostListByTask(t);
		if (hostList.size() == 0)
			return 0;
		DevAlarmLog alarmLog = new DevAlarmLog();

		for (DevHost host : hostList) {
			String content = "主机已失联时间大于告警值" + configService.selectConfigByKey("dev.host.offlineTimes") + "分钟";
			alarmLog.setType("OfflineWarn");
			alarmLog.setIp(host.getIp());
			alarmLog.setCreateTime(now);
			alarmLog.setGenerateTime(now);
			alarmLog.setContent(content);
			alarmLog.setCode(Md5Utils.hash(alarmLog.getType() + alarmLog.getContent()));
			alarmLogService.insertDevAlarmLog(alarmLog);
		}

		return devHostMapper.updateDevHostStatusByTask(t);
	}

	
	/**
	 * 更新主机状态
	 * 
	 * @param 无
	 * @return 状态码
	 */
	@Override
	public int deleteLogByTask() {
		int reserveDays =  Integer.valueOf(configService.selectConfigByKey("dev.task.reserveDays"));
		if (reserveDays == 0)
			return 0;
		int n = (-1) * reserveDays;
		Date now = com.xomily.common.utils.DateUtils.getNowDate();
		Date t = DateUtils.addDays(now, n);
		cpuLogMapper.deleteDevCpuLogByCreateTime(t);
		memLogMapper.deleteDevMemLogByCreateTime(t);
		discLogMapper.deleteDevDiscLogByCreateTime(t);
		netLogMapper.deleteDevNetLogByCreateTime(t);
		alarmLogMapper.deleteDevAlarmLogByCreateTime(t);	
		return 1;
	}
}
